package com.cn.lck.mapper;

import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Description: 使用注解方式提供通用的列表查询方法和增删改功能，并使用$拼接字符串方式，生成动态的SQL语句。
 * @Author: lck
 * @Date: 2020.10.22 14:51
 **/
@Mapper
public interface CommonDao {

    @Select("select * from ${tableName} limit #{start},#{size}")
    @ResultType(HashMap.class)
    List<HashMap> list(@Param("tableName") String tableName, @Param("start") int start, @Param("size") int size);

    @Select("select count(*) from ${tableName}")
    @ResultType(Integer.class)
    int listCount(@Param("tableName") String tableName);

    @Select("select * from ${tableName} where 1=1 ${where} limit #{start},#{size} ")
    @ResultType(HashMap.class)
    List<HashMap> listForWhere(@Param("tableName") String tableName, @Param("where") String where, @Param("start") int start, @Param("size") int size);

    @Select("select count(*) from ${tableName} where 1=1 ${where} ")
    @ResultType(Integer.class)
    int listCountForWhere(@Param("tableName") String tableName, @Param("where") String where);

    @Update("update ${tableName} set ${sets} where 1=1 ${where}")
    @ResultType(Integer.class)
    int update(@Param("tableName") String tableName, @Param("where") String where, @Param("sets") String sets);

    @Insert("insert into ${tableName} (${fileds}) values (${values})")
    @ResultType(Integer.class)
    int insert(@Param("tableName") String tableName, @Param("fileds") String fileds, @Param("values") String values);

    @Delete("delete from ${tableName} where 1 = 1 ${where} limit 1")
    @ResultType(Integer.class)
    int delete(@Param("tableName") String tableName, @Param("where") String where);
}
